package com.example.benji.mythreemenue;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private MyAddressBean myAddressBean;
    private RecyclerView rv_01;
    private RecyclerView rv_02;
    private RecyclerView rv_03;
    private List<FirstBean> firstData = new ArrayList<>();//第一层数据
    private List<MyAddressBean.DataBean.PositionBean.TrainLinesBean> trainLines;//地铁数据
    private List<MyAddressBean.DataBean.PositionBean.DistrictsBean> districts;//商圈数据
    private FirstAdapter firstAdapter;//第一层
    private TwoAdapterLine twoAdapterLine;//第二层地铁adapter
    private TwoAdapterBc twoAdapterBc;//第二层商圈adapter


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rv_01 = findViewById(R.id.rv_01);
        rv_02 = findViewById(R.id.rv_02);
        rv_03 = findViewById(R.id.rv_03);




        initeData();
    }

    private void initeData() {
        getDataFromNet();
        initOne();//初始化第一层
        initTwo();//初始化第二层



    }

    private void initOne() {
        FirstBean firstBean = new FirstBean();
        firstBean.setName("地铁");
        firstData.add(firstBean);
        FirstBean firstBean2 = new FirstBean();
        firstBean2.setName("商圈");
        firstData.add(firstBean2);
        firstAdapter = new FirstAdapter(null);
        firstAdapter.setNewData(firstData);
        rv_01.setLayoutManager(new LinearLayoutManager(this));
        rv_01.setAdapter(firstAdapter);

        firstAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (position ==0){
                    //如果点击的是地铁 那么第二层要显示地铁 否则显示商圈
                    rv_02.setAdapter(twoAdapterLine);
                    twoAdapterLine.setNewData(trainLines);
                    twoAdapterLine.notifyDataSetChanged();
                }else {
                    rv_02.setAdapter(twoAdapterBc);
                    twoAdapterBc.setNewData(districts);
                    twoAdapterBc.notifyDataSetChanged();
                }

            }
        });

    }

    private void initTwo() {
        rv_02.setLayoutManager(new LinearLayoutManager(this));
       twoAdapterLine = new TwoAdapterLine(null);
       twoAdapterBc = new TwoAdapterBc(null);
        rv_02.setAdapter(twoAdapterLine);

    }


    private void getDataFromNet() {
        OkGo.<String>post(Urls.getBuild)
                .params("user_id", 6405)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        myAddressBean = new Gson().fromJson(response.body(), MyAddressBean.class);
                        trainLines = myAddressBean.getData().getPosition().getTrainLines();
                        districts = myAddressBean.getData().getPosition().getDistricts();

                    }
                });


    }
}
